package locacao.integracao.dao.pstPostgreSQL;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import locacao.integracao.dao.FaturaDAO;
import locacao.negocio.to.FaturaTO;
import locacao.util.LocacaoUtil;

public class FaturaPgsqlDAO implements FaturaDAO {

    private ArrayList<FaturaTO> faturas;

    public FaturaPgsqlDAO(){
	   setFaturas(new ArrayList<FaturaTO>());
    }


    public void addOrUpdateFatura(FaturaTO fatura) {
    	Connection conn = null;
    	CallableStatement spManterFatura = null;

    	try {        	
          String sql = "{call locacao.manter_fatura(?,?,?,?)}";
          conn = ConnectionRentACar.getInstance().getConnection();
          
          spManterFatura = conn.prepareCall(sql);
          
          spManterFatura.setInt(1, fatura.getIdFatura());
          spManterFatura.setDate(2,  fatura.getDataEmissao());
          spManterFatura.setInt(3, (int) fatura.getValorTotal());
          spManterFatura.setInt(4, fatura.getIdLocacao());
          
          spManterFatura.execute();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        } finally {
          try {
            if (spManterFatura != null)
            	spManterFatura.close();

          } catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
    }

    /**
     *

    /**
     * @param fatura
     */
    public void excludeFatura( FaturaTO fatura )  {
    	Connection conn = null;
    	CallableStatement spDeleteFatura = null;

    	try {        	
          String sql = "{call locacao.delete_fatura(?)}";
          conn = ConnectionRentACar.getInstance().getConnection();
          
          spDeleteFatura = conn.prepareCall(sql);
          
          spDeleteFatura.setInt(1, fatura.getIdFatura());         
          
          spDeleteFatura.execute();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        } finally {
          try {
            if (spDeleteFatura != null)
            	spDeleteFatura.close();

          } catch (SQLException e) {
            System.out.println(e.getMessage());
          }
        }
    }


  
    /**
     * @return
     */
    public String[] findFaturaById(Integer idFatura, Integer idLocacao){
    	 Connection conn = null;
    	 Statement stmt = null;
    	 ResultSet rSet = null;
    	 String[] info = null;

    	 String sql = "SELECT  idfatura, dataemissao, valortotal, idlocacao ";
    	 sql += " FROM locacao.fatura WHERE idfatura =  " + idFatura + " and idlocacao = " + idLocacao;

    	 try {
    		 conn = ConnectionRentACar.getInstance().getConnection();
    		 stmt = conn.createStatement();
    		 rSet = stmt.executeQuery(sql);

    		 if (rSet != null) {    			 
    			 while (rSet.next()) {
    				 
    				info = new String[faturas.size()];
    	    		   info[0] = rSet.getInt("idfatura") 
    	       		   + " - " + rSet.getDate("dataemissao")
    	       		   + " - " + rSet.getFloat("valortotal")
    	       		   + " - " + rSet.getInt("idlocacao");		   
    			 }    			 
    		 }
			 return info;
			 
    	 } catch (Exception e) {
    		 System.out.println(e.getMessage());
    		 return null;
    	 } finally {
    		 try {
    			 if (rSet != null)
    				 rSet.close();
    			 if (stmt != null)
    				 stmt.close();
    		 } catch (SQLException e) {
    			 System.out.println(e.getMessage());
    		 }
    	 }
    }

    
    /**
     * @return
     */
    public String[] getInfoFaturas()
    {
        FaturaTO fatura;
        faturas = getFaturas();
        String[] info = new String[faturas.size()];
        Iterator iteratorFaturas;
        iteratorFaturas = faturas.iterator();
        int i=0;
        while ( iteratorFaturas.hasNext() )
        {
            fatura = (FaturaTO) iteratorFaturas.next();
            info[i] = fatura.getIdFatura() 
            		+ " - " + LocacaoUtil.formataDataStr(fatura.getDataEmissao())
            		+ " - " + fatura.getValorTotal()
            		+ " - " + fatura.getIdLocacao();
            i++;
        }
        return info;
    }


    /**
     * @return
     */
    public ArrayList<FaturaTO> getFaturas() {
    	 Connection conn = null;
    	 Statement stmt = null;
    	 ResultSet rSet = null;
    	 FaturaTO fatura = null;

    	 String sql = "SELECT  idfatura, dataemissao, valortotal, idlocacao ";
    	 sql += " FROM locacao.fatura ORDER BY idfatura";

    	 try {
    		 conn = ConnectionRentACar.getInstance().getConnection();
    		 stmt = conn.createStatement();
    		 rSet = stmt.executeQuery(sql);

    		 if (rSet != null) {
    			 faturas = new ArrayList();
    			 while (rSet.next()) {
    				 
    				 fatura = new FaturaTO();
    				 
    				 fatura.setIdFatura(rSet.getInt("idfatura"));
    				 fatura.setDataEmissao(rSet.getDate("dataemissao"));
    				 fatura.setValorTotal(rSet.getFloat("valortotal"));
    				 fatura.setIdLocacao(rSet.getInt("idlocacao"));
    				     				 
    				 faturas.add(fatura);
    			 }
    		 }
    	 } catch (Exception e) {
    		 System.out.println(e.getMessage());
    	 } finally {
    		 try {
    			 if (rSet != null)
    				 rSet.close();
    			 if (stmt != null)
    				 stmt.close();
    		 } catch (SQLException e) {
    			 System.out.println(e.getMessage());
    		 }
    	 }
    	 return faturas;
    }

    private void setFaturas(ArrayList<FaturaTO> faturas) {
    	this.faturas = faturas;
    }
    
}
